package com.wiserun.RestFul.service;

import com.wiserun.RestFul.mapper.PncsOrganizationMapper;
import com.wiserun.RestFul.model.Organization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
public class PncsOrganizationService {

    @Autowired
    private PncsOrganizationMapper pncsOrganizationMapper;



    //新增一条记录
    public int addOgranization(Organization org){
        int num = 0 ;
        if(org != null) {
            num = pncsOrganizationMapper.addOrganization(org);
        }
        return  num;
    }
    //批量增加多条记录
    @Transactional
    public HashMap<String,Integer> batchAddOgranization(List<Organization> organizationList){
        int num_add = 0;//新增的数量
        int num_update = 0;//修改的数量
        int sum_add = 0;//新增的数量汇总
        int sum_update = 0;//新增的数量汇总
        HashMap<String,Integer> counts = new HashMap<>();
        //修改的数量汇总
        if(organizationList == null){
            counts.put("sum_add",0);
            counts.put("sum_update",0);
            return counts;
        }
        // oid存在的情况下不需要插入
        for(Organization org :organizationList ){
            int count = pncsOrganizationMapper.countByOid(org.getOid());
            if(count !=0) {//数据库有重复数据
                num_update= pncsOrganizationMapper.updateOrg(org);//更新重复数据信息
                sum_update+=num_update;
                continue;
            }
            num_add = pncsOrganizationMapper.addOrganization(org);
            sum_add+=num_add;

        }
        System.out.println("------------------------------新增总条数===="+sum_add);
        counts.put("sum_add",sum_add);
        counts.put("sum_update",sum_update);
        return counts;
    }

    //根据oid查询单条记录
    public Organization queryOrgByOid(String oid){
        if(oid == null) {
            return  null;
        }
       return pncsOrganizationMapper.queryOrgByOid(oid);
    }

    //查询所有org记录
    public  List<Organization> getOrganization(){
        return pncsOrganizationMapper.getOrganization();
    }



}
